package o;

import android.content.Context;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public final class cmf implements cly {
    private c ʽ;
    private int ˊ;
    private int ˋ;
    private File ˎ;
    private boolean ˏ;
    private String ॱ;

    static class c {
        int ˋ;
        File ˏ;

        c(File file, int i) {
            this.ˏ = file;
            this.ˋ = i;
        }

        static c ˊ(File file, String str) {
            try {
                String[] split = str.split("-");
                if (split.length != 3) {
                    return null;
                }
                return new c(new File(file, str), Integer.parseInt(split[1]));
            } catch (NumberFormatException e) {
                return null;
            }
        }
    }

    public void ˋ(Context context, clv o_clv) {
        if (context == null || o_clv == null) {
            throw new NullPointerException("context or param must not be null.");
        }
        this.ˎ = ॱ(context, o_clv.ˊ());
        this.ॱ = o_clv.ʻ();
        this.ˋ = o_clv.ॱॱ();
        this.ˊ = o_clv.ʼ();
        boolean z = this.ˎ != null && (this.ˎ.isDirectory() || this.ˎ.mkdirs());
        this.ˏ = z;
        if (this.ˏ) {
            List ॱ = ॱ(this.ˎ);
            ॱ(ॱ);
            ˋ(ॱ);
            return;
        }
        Log.e("FileNode", "Failed to initialize the log-directory.");
    }

    public void ॱ(String str, int i, String str2, String str3, Throwable th) {
        if (!TextUtils.isEmpty(str) && this.ˏ && this.ʽ != null && this.ˎ != null) {
            if (this.ʽ.ˏ.length() + ((long) str.length()) > ((long) this.ˋ)) {
                List ॱ = ॱ(this.ˎ);
                ॱ(ॱ);
                ˋ(ॱ);
            }
            this.ˏ = ˎ(this.ʽ.ˏ, str + "\n");
        }
    }

    void ॱ(List<c> list) {
        int i = 0;
        if (!list.isEmpty()) {
            i = ((c) list.get(list.size() - 1)).ˋ;
        }
        i = ॱ(i);
        this.ʽ = new c(new File(this.ˎ, this.ॱ + "-" + i + "-" + Process.myPid() + ".log"), i);
        list.add(this.ʽ);
    }

    void ˋ(List<c> list) {
        int size = list.size() - this.ˊ;
        for (int i = 0; i < size; i++) {
            Iterator it = list.iterator();
            if (it.hasNext()) {
                c cVar = (c) it.next();
                it.remove();
                if (!cVar.ˏ.delete()) {
                    Log.e("FileNode", "Failed to delete the log-file.");
                }
            }
        }
    }

    static int ॱ(int i) {
        int i2 = i + 1;
        return i2 > 0 ? i2 : 1;
    }

    static List<c> ॱ(File file) {
        final List<c> arrayList = new ArrayList();
        file.listFiles(new FilenameFilter() {
            public boolean accept(File file, String str) {
                if (new File(file, str).isFile()) {
                    c ˊ = c.ˊ(file, str);
                    if (ˊ != null) {
                        arrayList.add(ˊ);
                    }
                }
                return false;
            }
        });
        Collections.sort(arrayList, new Comparator<c>() {
            public /* synthetic */ int compare(Object obj, Object obj2) {
                return ˏ((c) obj, (c) obj2);
            }

            public int ˏ(c cVar, c cVar2) {
                return cVar.ˋ - cVar2.ˋ;
            }
        });
        return arrayList;
    }

    static File ॱ(Context context, String str) {
        File filesDir;
        if (str.startsWith("/FilesDir/")) {
            filesDir = context.getFilesDir();
            if (filesDir != null) {
                return new File(filesDir, str.substring("/FilesDir/".length()));
            }
            return null;
        } else if (!str.startsWith("/ExternalFilesDirs/")) {
            return new File(str);
        } else {
            filesDir = context.getExternalFilesDir(null);
            if (filesDir != null) {
                return new File(filesDir, str.substring("/ExternalFilesDirs/".length()));
            }
            return null;
        }
    }

    private static boolean ˎ(File file, String str) {
        Closeable fileOutputStream;
        Closeable bufferedOutputStream;
        Closeable outputStreamWriter;
        Closeable closeable;
        Throwable th;
        Closeable closeable2 = null;
        try {
            fileOutputStream = new FileOutputStream(file, true);
            try {
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                try {
                    outputStreamWriter = new OutputStreamWriter(bufferedOutputStream, "UTF-8");
                } catch (FileNotFoundException e) {
                    closeable = null;
                    outputStreamWriter = fileOutputStream;
                    closeable2 = bufferedOutputStream;
                    try {
                        Log.w("FileNode", "Exception when writing the log file.");
                        ˎ(closeable);
                        ˎ(closeable2);
                        ˎ(outputStreamWriter);
                        return false;
                    } catch (Throwable th2) {
                        fileOutputStream = outputStreamWriter;
                        Closeable closeable3 = closeable;
                        th = th2;
                        bufferedOutputStream = closeable2;
                        closeable2 = closeable3;
                        ˎ(closeable2);
                        ˎ(bufferedOutputStream);
                        ˎ(fileOutputStream);
                        throw th;
                    }
                } catch (IOException e2) {
                    try {
                        Log.w("FileNode", "Exception when writing the log file.");
                        ˎ(closeable2);
                        ˎ(bufferedOutputStream);
                        ˎ(fileOutputStream);
                        return false;
                    } catch (Throwable th3) {
                        th = th3;
                        ˎ(closeable2);
                        ˎ(bufferedOutputStream);
                        ˎ(fileOutputStream);
                        throw th;
                    }
                }
                try {
                    outputStreamWriter.write(str);
                    outputStreamWriter.flush();
                    ˎ(outputStreamWriter);
                    ˎ(bufferedOutputStream);
                    ˎ(fileOutputStream);
                    return true;
                } catch (FileNotFoundException e3) {
                    closeable = outputStreamWriter;
                    closeable2 = bufferedOutputStream;
                    outputStreamWriter = fileOutputStream;
                    Log.w("FileNode", "Exception when writing the log file.");
                    ˎ(closeable);
                    ˎ(closeable2);
                    ˎ(outputStreamWriter);
                    return false;
                } catch (IOException e4) {
                    closeable2 = outputStreamWriter;
                    Log.w("FileNode", "Exception when writing the log file.");
                    ˎ(closeable2);
                    ˎ(bufferedOutputStream);
                    ˎ(fileOutputStream);
                    return false;
                } catch (Throwable th4) {
                    th = th4;
                    closeable2 = outputStreamWriter;
                    ˎ(closeable2);
                    ˎ(bufferedOutputStream);
                    ˎ(fileOutputStream);
                    throw th;
                }
            } catch (FileNotFoundException e5) {
                closeable = null;
                outputStreamWriter = fileOutputStream;
                Log.w("FileNode", "Exception when writing the log file.");
                ˎ(closeable);
                ˎ(closeable2);
                ˎ(outputStreamWriter);
                return false;
            } catch (IOException e6) {
                bufferedOutputStream = null;
                Log.w("FileNode", "Exception when writing the log file.");
                ˎ(closeable2);
                ˎ(bufferedOutputStream);
                ˎ(fileOutputStream);
                return false;
            } catch (Throwable th5) {
                th = th5;
                bufferedOutputStream = null;
                ˎ(closeable2);
                ˎ(bufferedOutputStream);
                ˎ(fileOutputStream);
                throw th;
            }
        } catch (FileNotFoundException e7) {
            closeable = null;
            outputStreamWriter = null;
            Log.w("FileNode", "Exception when writing the log file.");
            ˎ(closeable);
            ˎ(closeable2);
            ˎ(outputStreamWriter);
            return false;
        } catch (IOException e8) {
            bufferedOutputStream = null;
            fileOutputStream = null;
            Log.w("FileNode", "Exception when writing the log file.");
            ˎ(closeable2);
            ˎ(bufferedOutputStream);
            ˎ(fileOutputStream);
            return false;
        } catch (Throwable th6) {
            th = th6;
            bufferedOutputStream = null;
            fileOutputStream = null;
            ˎ(closeable2);
            ˎ(bufferedOutputStream);
            ˎ(fileOutputStream);
            throw th;
        }
    }

    private static void ˎ(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                Log.w("FileNode", "Exception when closing the closeable.");
            }
        }
    }
}
